//
//  DJLoginViewController.m
//  掌中会议通
//
//  Created by wanghy on 15/12/13.
//  Copyright © 2015年 wanghy. All rights reserved.
//

#import "DJLoginViewController.h"
#import "DJRegistController.h"
#import "MBProgressHUD.h"
#import "MBProgressHUD+Ex.h"
#import "DJHViewController.h"
#define Keypass @"passDict"
#import "EaseMob.h"
#import "DJTableBarController.h"


@interface DJLoginViewController ()<MBProgressHUDDelegate> {
    MBProgressHUD *HUD;
    long long expectedLength;
    long long currentLength;
}

//盛放账号和密码的View
@property (weak, nonatomic) IBOutlet UIView *loginView;

//登陆按钮
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

//注册按钮
@property (weak, nonatomic) IBOutlet UIButton *registBtn;

//记住密码开关
@property (weak, nonatomic) IBOutlet UILabel *remPaSwitch;

//忘记密码按钮
@property (weak, nonatomic) IBOutlet UIButton *forgetPassBtn;
//账号框
@property (weak, nonatomic) IBOutlet UITextField *nameTextFled;
//密码框
@property (weak, nonatomic) IBOutlet UITextField *passwordTextFled;

@end

@implementation DJLoginViewController
/*
 忘记密码点击事件
 */
- (IBAction)didClickforgetPassBtn:(id)sender {
}

/*
 登陆按钮点击事件
 */
- (IBAction)didClickLoginBtn:(id)sender {
    HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:HUD];
    
    HUD.color = [UIColor colorWithRed:0.23 green:0.50 blue:0.82 alpha:0.90];
    
    HUD.delegate = self;
    [HUD show:YES];
    
    [[EaseMob sharedInstance].chatManager asyncLoginWithUsername:self.nameTextFled.text password:self.passwordTextFled.text completion:^(NSDictionary *loginInfo, EMError *error) {
        if (!error && loginInfo) {
              [[EaseMob sharedInstance].chatManager setIsAutoLoginEnabled:YES];
            NSLog(@"登陆成功");
            NSLog(@"----------%@",loginInfo);
            
            
            
            [[NSNotificationCenter defaultCenter]postNotificationName:@"loginSuccess" object:nil];
            
            
        }else{
            [HUD hide:YES];
            
            [ MBProgressHUD showError:@"账号或密码错误"];
        }
    } onQueue:nil];
    
    
    
}




/*
 注册按钮点击事件
 */
- (IBAction)didClickRegestBtn:(id)sender {
    DJRegistController * vc = [[DJRegistController alloc]init];
    
    [self presentViewController:vc animated:YES completion:nil];
    
}


#pragma mark -

- (void)viewDidLoad {
    [super viewDidLoad];
    //    设置圆角
    
    self.loginView.layer.cornerRadius = 15;
    self.loginView.layer.masksToBounds = YES;
    self.loginBtn.layer.cornerRadius = 15;
    self.loginBtn.layer.masksToBounds = YES;
    self.registBtn.layer.cornerRadius = 15;
    self.registBtn.layer.masksToBounds = YES;
    
    /**
     给两个textfled添加监听事件。当两个文本框中的值发生改变时立即调用changemethod。
     */
    [self.nameTextFled addTarget:self
                          action:@selector(changeMethod)
                forControlEvents:UIControlEventEditingChanged];
    [self.passwordTextFled addTarget:self
                              action:@selector(changeMethod)
                    forControlEvents:UIControlEventEditingChanged];
    /**
     *  登录按钮默认是不能点击的。
     */
    self.loginBtn.enabled = NO;
    
    // 解决文本框默认值 登陆不可用的问题
    [self changeMethod];
    
//    自动登录
    BOOL isAutoLogin = [[EaseMob sharedInstance].chatManager isAutoLoginEnabled];
    if (isAutoLogin) {
    
    DJTableBarController*    tabVc = [[DJTableBarController alloc]init];
        
        [UIApplication sharedApplication].keyWindow.rootViewController =tabVc;
    }
}

/**
 *  只有两个文本框同时有值的时候才能点击登陆按钮
 */
- (void)changeMethod {
    
    self.loginBtn.enabled =
    (self.nameTextFled.text.length && self.passwordTextFled.text.length);
}

@end
